package com.example.cloud.service.basic.info.impl;

import com.example.cloud.dao.basic.info.CourseDetailsRepository;
import com.example.cloud.dao.basic.info.CourseRepository;
import com.example.cloud.dao.mongdb.basic.info.MongoCourseDetailsRepository;
import com.example.cloud.dao.mongdb.basic.info.MongoCourseRepository;
import com.example.cloud.model.basic.Course;
import com.example.cloud.model.basic.CourseDetails;
import com.example.cloud.service.basic.info.CourseDetailsService;
import com.example.cloud.service.basic.info.CourseService;
import com.example.cloud.utils.BusinessUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by wangtao on 2018/6/6.
 */
@Service
public class CourseDetailsServiceImpl implements CourseDetailsService {

    private static final Logger logger = Logger.getLogger(CourseDetailsServiceImpl.class);

    @PersistenceContext
    EntityManager em;

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    private CourseService courseService;

    @Autowired
    private CourseRepository courseRepository;

    @Autowired
    private CourseDetailsRepository courseDetailsRepository;

    @Autowired
    private MongoCourseRepository mongoCourseRepository;

    @Autowired
    private MongoCourseDetailsRepository mongoCourseDetailsRepository;

    @Override
    public List<CourseDetails> findAllByCourseId(Long courseId) {
        return mongoCourseDetailsRepository.findByCourseIdAndDeleteFlag(courseId, 0);
    }

    @Override
    @Transactional
    public CourseDetails doSave(CourseDetails record) {
        // 物理保存
        BusinessUtil.appendRecord(record);
        record = courseDetailsRepository.save(record);
        // 同步缓存
        mongoCourseDetailsRepository.save(record);
        // 刷新疗程缓存
        Course course = courseService.findOneFromCache(record.getCourseId());
        course.setOriginalPrice(record.getPrice().multiply(new BigDecimal(record.getQuantity())));
        // 同步疗程
        courseService.doSave(course);
        return record;
    }

    @Override
    @Transactional
    public void doDelete(List<Long> ids, Long courseId) {
        // 逻辑删除
        StringBuilder sql = new StringBuilder("update basic_course_details set delete_flag = 1 where id in(:ids)");
        Query query = em.createNativeQuery(sql.toString());
        query.setParameter("ids", ids);
        query.executeUpdate();
        // 删除缓存
        mongoCourseDetailsRepository.deleteByIdIn(ids);
        // 同步疗程
        List<CourseDetails> details = mongoCourseDetailsRepository.findByCourseIdAndDeleteFlag(courseId, 0);
        BigDecimal price = BigDecimal.ZERO;
        details.forEach(courseDetails -> price.add(courseDetails.getPrice()));
        Course course = mongoCourseRepository.findOne(courseId);
        course.setOriginalPrice(price);
        BusinessUtil.appendRecord(course);
        courseRepository.save(course);
        mongoCourseRepository.save(course);
    }

    @Override
    public void loadCache() {
        List<CourseDetails> list = courseDetailsRepository.findAll();
        if(!list.isEmpty()){
            mongoCourseDetailsRepository.save(list);
            logger.info("*******************疗程明细已全部同步缓存*******************");
        }
    }

    @Override
    public void deleteByCourseId(List<Long> ids) {
        // 逻辑删除
        StringBuilder sql = new StringBuilder("update basic_course_details set delete_flag = 1 where course_id in(:ids)");
        Query query = em.createQuery(sql.toString());
        query.setParameter("ids", ids);
        query.executeUpdate();
        // 删除缓存
        mongoCourseDetailsRepository.deleteByCourseIdIn(ids);
    }
}
